(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('lfe.monitor.wfTxnProcess.listController', listController);
	listController.$inject = ['$rootScope', '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'ajaxServiceFactory'];
	function listController($rootScope, $scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, ajaxServiceFactory) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("wffault");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		var ajaxService = ajaxServiceFactory.buildAjaxService({
			getItemTypeOptions : {
				url : 'do/lfe/dataList/itemType',
				method : 'get'
			}
		});

		ctrl.init = function() {
			ajaxService.getItemTypeOptions().then(function(response) {
				ctrl.itemKeyOptions = response.data;
				
				ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
				ctrl.gridOptions.data = 'vm.searchCriteria.list';
				ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
				ctrl.search();
			});
		};

		ctrl.view = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$scope.go('lfe.monitor.wfTxnProcess.view', ctrl.selectedRows[0]);
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			}, angular.noop);
		};
		
		ctrl.stop = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			var s=ctrl.selectedRows[0].instanceId.indexOf("^");
			var id=ctrl.selectedRows[0].instanceId.substring(0,s);
			modalService.openConfirmDialogs({
				'messages' : [ 'Are you sure you want to stop the process '+id+'?' ],
				'btnCallback' : {
					'ok':function(response) {
						authService.stop(ctrl.selectedRows[0]).then(stopSuccessful, stopFailed);
					}
				}
			});
		};
		
		function stopSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'Stop successfully!' ],
				'btnCallback' : {
					'close' : function() {
						ctrl.search();
					}
				}
			});
		}

		function stopFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
			$rootScope.initAuditOrNoteParams("","");
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);